package com.soft.idc.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class StaticRimFilter extends Controller implements Filter {

	public void clear() {
		initVersion();
		this.renderJson("result", 0);
	}

	private static String version;

	public static void initVersion() {
		Record vo = Db.findFirst("select * from idc_version;");
		version = vo.getInt("idc_version") + "";
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		initVersion();
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub

		HttpServletRequest httpRequest = (HttpServletRequest) request;
		// String uri = httpRequest.getServletPath();
		String uriVersion = request.getParameter("v");
		if (uriVersion == null || "".equals(uriVersion)) {
			((HttpServletResponse) response).sendRedirect(httpRequest.getRequestURI() + "?v=" + version);
		} else {
			chain.doFilter(httpRequest, response);
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
